<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 18/12/22
 * Time: 上午10:55
 */

//require_once './phpword/PHPWord.php';
//require_once DRUPAL_ROOT . '/sites/app/plugin/word/phpwordDir/PHPWord.php';
//require_once DRUPAL_ROOT . '/sites/app/plugin/word/phpwordDir/PHPWord/IOFactory.php';
require_once 'f:/localproject/test/word/phpwordDir/PHPWord.php';
require_once 'f:/localproject/test/word/phpwordDir/PHPWord/IOFactory.php';
class App_Plugin_Word_PHPWordPlugin{

    //根据模板下载合同信息
    public function _down_compact($order_info){
        //Libs_Log_Logger::outputLog($order_info,'word.log');
        //初始化phpword类
        $PHPWord = new PHPWord();
        //设置模板数据(compact1 没有章/未支付;compact2 有章/已支付)
        if($order_info[0] == 1){
            $docxinfo = 'compact1.docx';
            //$info_len = 12;
        }else{
            $docxinfo = 'compact2.docx';
            //$info_len = 15;
        }
        $document = $PHPWord->loadTemplate($docxinfo);
        for($i = 1;$i <= 15;$i++){
            $document->setValue('Value'.$i, $order_info[$i]);
        }
//        $document->setValue('Value1', '1201812225293224294');           //合同编号
//        $document->setValue('Value2', '代理废旧塑料');                    //甲方
//        $document->setValue('Value3', '重庆市重庆区奥斯卡的哈利实打实');    //收件地址
//        $document->setValue('Value4', '张三丰');                         //联络人
//        $document->setValue('Value5', '15633336666');                   //手机
//        $document->setValue('Value6', '123@163.com');                   //电子邮箱
//        $document->setValue('Value7', '李四');                           //客户经理姓名
//        $document->setValue('Value8', '10022223333');                   //客户经理电话
//        $document->setValue('Value9', '订单信息担保交易');                //订单信息


        $md5            = md5(time().mt_rand(1000, 9999));
        $tmpFileName    = substr($md5, 0, 8).'-'.substr($md5, 10, 4).'-'.mt_rand(1000, 9999).'-'.substr($md5, 20, 12);
        $gallery        = '/document/word/';
        //$fileroot       = PLUM_DIR_UPLOAD . $gallery ;
        //保存临时文件
        //$document->save($fileroot.$tmpFileName.'.docx');

        //$file = $fileroot.$tmpFileName.'.docx';

//        //word转pdf
        //$this->wordtopdf($file, $tmpFileName);

        // 下载Word文件
        //$this->down_word($file);

        //word转pdf  先转化为html

    }

    //下载Word文件
    public function down_word($file){
        ob_start(); //打开缓冲区
        $fp = fopen($file,"r");
        $file_size = filesize($file);
        $downFileName = '合同书.docx';

        header("Cache-Control: public");
        header("Content-type: application/octet-stream");
        header("Accept-Ranges: bytes");
        header("Content-Disposition: attachment; filename={$downFileName}");
        header("Pragma:no-cache");
        header("Expires:0");
        $buffer = 1024;
        $file_count = 0;
        //向浏览输出回数据
        while(!feof($fp) && $file_count < $file_size){
            $file_con = fread($fp,$buffer);
            $file_count += $buffer;
            echo $file_con;
        }
        ob_end_flush();//输出全部内容到浏览器
    }

    //word转pdf
    public function wordtopdf($file, $tmpFileName){
        //$gallery  = '/document/pdf/';
        //$fileroot = PLUM_DIR_UPLOAD . $gallery ;
        $fileroot = 'f:/localproject/test/upload/pdf';

        $pdfPath  = $fileroot;//pdf文件保存的目录
        $pdfHomePath = $fileroot;//项目目录或项目中pdf保存的目录
        $doc_path = $file;//word文件地址
        if(!is_dir($pdfPath)){
            mkdir($pdfPath,0777,true);
        }
        //生成pdf
        shell_exec("export HOME={$pdfHomePath} && libreoffice --headless --invisible --convert-to pdf:writer_pdf_Export {$doc_path} --outdir {$pdfPath}");
        //下载pdf
        $pdfName = $tmpFileName.".pdf";
        $pdfFile = $pdfHomePath.$pdfName;
        $filesize   = filesize($pdfFile);
        header("Content-Type: application/octet-stream");
        header("Accept-Ranges: bytes");
        header("Accept-Length: ".$filesize);
        header("Content-Disposition: attachment; filename=".$pdfName);
        //plum_send_http_header("Content-type: application/octet-stream");
        //plum_send_http_header("Accept-Ranges: bytes");
        //plum_send_http_header("Accept-Length:".$filesize);
        //plum_send_http_header("Content-Disposition: attachment; filename=".$pdfName);
        // PDF 源在 original.pdf 中
        readfile($pdfFile);
        die();
    }

}
